<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	//编辑，新增弹出框
	var detailWin = {}, curentData = null, currentIsAddNew;
	var detailEditor = {};
	jQuery.metadata.setType("attr", "validate"); 
	function FF_ShowDialog(data, isAddNew, moduleId)
	{
		var fd = $("#"+moduleId+"formdetail");
		curentData = data;
		currentIsAddNew = isAddNew;
		if (detailWin[moduleId]){
			detailWin[moduleId].show(); 
		}else{
			for(var key in childData){
				if(childData[key].module.moduleId == moduleId){
					detailEditor[moduleId] = initForm(childData[key].module.moduleFields, fd, dicObj, {'width':200, 'prefix':'SDdetail_', 'suffix':''}, 1 , eventObj[childData[key].module.module]);
					detailWin[moduleId] = $.ligerDialog.open({
						title: '编辑',
						target: $("#"+moduleId+"detail"),
						isShade : true,
						onClose : function() {
							detailWin[moduleId] = null;
						},
						width: 400, 
						height: 300,
						buttons: [
							{ text: '保存', onclick: function () { FF_DetailSave(moduleId); } },
							{ text: '取消', onclick: function () { detailWin[moduleId].hide(); } }
						]
					});
				}
			}
		}
		var editor = detailEditor[moduleId];
		var form = editor.form;
		var fields = editor.fields;
		if (curentData) {
			for ( var k in fields) {
				var name = fields[k].code;
				var value = curentData[fields[k].code];
				if(relatedField && relatedField.hasOwnProperty(name)){
					form.find("input[name='" + editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(relatedField[name]);
				}else if(editor.element[name]){
					if((editor.element[name].type == "comboBox" || editor.element[name].type == "checktree" || editor.element[name].type == "radio") && fields[k].options.valueFieldID){
						var valueFieldId = fields[k].options.valueFieldID.replace(editor.options.prefix,"");
						value = curentData[valueFieldId];
					}
					if(value!=null && value!=""){
						if(editor.element[name].type == "radio"){
							editor.element[name].element.clear();
						}
						editor.element[name].element._setValue(value);
					}else{
						if(editor.element[name].type != "radio"){
							editor.element[name].element.clear();
						}
					}
				}else{
					if(fields[key].type == "textarea"){
						form.find("textarea[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(value);
					}else{
						form.find("input[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(value);
					}
				}
			}
		} else {
			for ( var k in fields) {
				var name = fields[k].code;
				if(relatedField && relatedField.hasOwnProperty(name)){
					form.find("input[name='" + editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(relatedField[name]);
				}else if(editor.element[name]){
					if(editor.element[name].type != "radio"){
						editor.element[name].element.clear();
					}
				}else{
					if(fields[key].type == "textarea"){
						form.find("textarea[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val("");
					}else{
						form.find("input[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val("");
					}
				}
			}
		}
		
		for(var key in childData){
			if(childData[key].module.moduleId == moduleId){
				if(eventObj[childData[key].module.module] && eventObj[childData[key].module.module].initForm){
					eventObj[childData[key].module.module].initForm(data,moduleId);
				}
				return false;
			}
		}
		
		LG.validate(fd);
	}
	
	function FF_DetailSave(moduleId){
		var fd = $("#"+moduleId+"formdetail");
		fd.attr("action","${baseURL}/jdbc/common/basecommonsave/save.do?strategy=${strategy}&mdId=" + moduleId);
		LG.submitForm(fd, function (data) {
			if (data && data.hasOk) {  
				childrenDetailsGrid[moduleId].loadData();
				detailWin[moduleId].hide();
				win.$.ligerDialog.success('操作成功!');
			} else { 
				win.$.ligerDialog.error("操作失败!失败信息如下:\n"+data.message+"\n如您对以上信息有疑问，请联系管理人员!");
			}
		});
	}
</script>
